<?php  
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;

class User extends Controller
{
	// 用户注册
	public function register()
	{
		return view('register');
	}
	public function register_add()
	{
        $time = time();
        $usersql = "select * from man_user";
        $userinfo = Db::query($usersql);
        $data = input('post.');
        if(!captcha_check($data['code'])) {
            // 校验失败
            $this->error('验证码不正确');
        }else{
            if ($userinfo[0]['u_name']==$data['u_name']){
                $this->error('该用户名已存在');
            }else{
                $sql = "insert into man_user values ('','','".$data['u_name']."','".$data['u_pwd']."','','$time','','','')";
                $res = Db::execute($sql);
                if ($res){
                    $this->success('注册成功');
                }
            }
        }
	}
	// 登录
    public function login()
    {
        return view('login');
    }
    public function loginOn()
    {
        $data = input('post.');
        $usersql = "select * from man_user where u_name = '".$data['u_name']."'";
        $userinfo = Db::query($usersql);
        if(!captcha_check($data['code'])) {
            // 校验失败
            $this->error('验证码不正确');
        }else{
            if ($data['u_pwd']==$userinfo[0]['u_pwd']){
                cookie('name',$userinfo[0]['u_name'],3600);//两小时后过期
                cookie('u_id',$userinfo[0]['u_id'],3600);
                $this->success('登录成功','index/index');
            }else{
                $this->error('密码错误');
            }
        }
    }
    // 登出
    public function loginOut()
    {

        // 清除session（当前作用域）
        // Session::clear();
        // Cookie::clear();
        cookie('name',null);
        cookie('u_id',null);
        return $this->redirect('user/login');
    }

    // 我的个人中心
    public function userindex()
    {
        $typeData = $this->alltype();
        return view('userindex',['typeData'=>$typeData]);
    }
    // 我的个人资料
    public function usersub()
    {
        $typeData = $this->alltype();
        return view('usersub',['typeData'=>$typeData]);
    }
    // 我的收藏
    public function collect()
    {
        $typeData = $this->alltype();
        return view('collect',['typeData'=>$typeData]);
    }
    // 我的订单
    public function usercenter()
    {
        $typeData = $this->alltype();
        return view('usercenter',['typeData'=>$typeData]);
    }

    // 申请退货
    public function usertui()
    {
        $typeData = $this->alltype();
        return view('usertui',['typeData'=>$typeData]);
    }

    // 收货地址
    public function newPath()
    {
        if($this->request->isAjax()){
            $area_id = input("get.area_id");
            $sql = "select * from man_area where area_parent_id=$area_id";
            $areaData = Db::query($sql);
            $return = array('areaData'=>$areaData);
            echo json_encode($return);
            die;
        }
        $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
        $areaData = Db::query($sql);

        $typeData = $this->alltype();
        return view('newPath',['typeData'=>$typeData,'areaData'=>$areaData]);
    }
    // 收货地址添加
    public function newPathAdd()
    {
        if($this->request->isAjax()){
            $area_id = input("get.area_id");
            $sql = "select * from man_area where area_parent_id=$area_id";
            $areaData = Db::query($sql);
            $return = array('areaData'=>$areaData);
            echo json_encode($return);
            die;
        }
        $sql = "select * from man_area where area_parent_id=0";
        $areaData = Db::query($sql);

        $typeData = $this->alltype();
        return view('newPath',['typeData'=>$typeData,'areaData'=>$areaData]);
    }


    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }

    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}


?>